#include "LCOMIndex.h"
#include "TKEIndex.h"
#include "../std.h"
#include "../Widgets/Colours.h"
#include "../Widgets/UIBuilder.h"
#include "../Utilities/trim.h"
#include "../Utilities/Convert.h"


LCOMIndex::LCOMIndex(){

		mProcessor = new LRESProcessor();

		isGetData = false;

		mLayout = createMainLayout(getStringNotIterative(MAIN_TEXT_START + 3), getStringNotIterative(L_SELECTCOMPANY), " ",  getStringNotIterative(L_BACK));

		mItems = (ListBox*)mLayout->getChildren()[1];

		mSoftkey = (Widget*)mLayout->getChildren()[2];

		this->setMain(mLayout);
}

LCOMIndex::~LCOMIndex(){
	delete mLayout;
}

void LCOMIndex::keyPressEvent(int keyCode, int nativeCode)
{
	switch(keyCode)
	{
		case MAK_UP:
		case MAK_2:
			mItems->selectPreviousItem();
			break;
		case MAK_DOWN:
		case MAK_8:
			mItems->selectNextItem();
			break;
		case MAK_SOFTRIGHT:
		case MAK_6:
			APPCONTROLLER->show("MainScreen");
			break;
		case MAK_FIRE:
		case MAK_SOFTLEFT:
			TKEIndex* screen= (TKEIndex*)APPCONTROLLER->getScreen("TKEIndex");
			LRESItemWidget* widget = (LRESItemWidget*)mItems->getChildren()[mItems->getSelectedIndex()];
			screen->setCompany(widget->getId(), widget->getCompanyName());
			APPCONTROLLER->show("TKEIndex");
			break;
	}
}

void LCOMIndex::itemReceived(LRESItem& item){
	APPCONTROLLER->mLRESList.add(item);
	int itemToday = stringToInteger(item.istoday);
	if (itemToday == 1){
		LRESItemWidget* newItem = new LRESItemWidget(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTITEM, mItems);

		newItem->setCompanyName(item.companyname);
		newItem->setCompanyFont(APPCONTROLLER->getFont("default"));
		//newItem->setDrawDate(item.drawdate);
		//newItem->setDrawDateFont(APPCONTROLLER->getFont("default"));

		newItem->setId(item.companyid);
		newItem->setSmsCnt(item.smscnt);
		newItem->setShortCode(item.shortcode);
		newItem->setIsOod(item.outofdate);

		newItem->setSkin(APPCONTROLLER->getSkin("default"));
		//newItem->addWidgetListener(this);

		mItems->setSelectedIndex(0);
		mItems->getChildren()[0]->setSelected(true);
	}
}

void LCOMIndex::triggered(Widget* sender)
{
	//Act as if fire has been pressed
	//keyPressEvent(MAK_FIRE, 0);
}

void LCOMIndex::show(){
	if (!APPCONTROLLER->mIsGetLRES){
		Label* l = (Label*)mSoftkey->getChildren()[1];
		l->setCaption("Loading");
		l->update();
		mProcessor->addRequest(APPCONTROLLER->getUrl(), this);
	}else if (APPCONTROLLER->mLRESList.size() > 0){
		//lprintfln("just have item");
		if (mItems->getChildren()[0] != NULL) mItems->setSelectedIndex(0);
		mItems->clear();
		for(int i = 0; i< APPCONTROLLER->mLRESList.size(); i++){
			LRESItem item = APPCONTROLLER->mLRESList[i];
			int itemToday = stringToInteger(item.istoday);
			if (itemToday == 1){
				LRESItemWidget* newItem = new LRESItemWidget(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTITEM, mItems);

				newItem->setCompanyName(item.companyname);
				newItem->setCompanyFont(APPCONTROLLER->getFont("default"));
				//newItem->setDrawDate(item.drawdate);
				//newItem->setDrawDateFont(APPCONTROLLER->getFont("default"));

				newItem->setId(item.companyid);
				newItem->setSmsCnt(item.smscnt);
				newItem->setShortCode(item.shortcode);
				//newItem->setResult(item.result);
				//newItem->setIsToday(item.istoday);
				newItem->setIsOod(item.outofdate);

				newItem->setSkin(APPCONTROLLER->getSkin("default"));
				//newItem->addWidgetListener(this);

				mItems->setSelectedIndex(0);
				mItems->getChildren()[0]->setSelected(true);
			}
		}
	}
	Screen::show();
}

void LCOMIndex::processingComplete(){
	APPCONTROLLER->mIsGetLRES = true;
	Label* l = (Label*)mSoftkey->getChildren()[1];
	l->setCaption(" ");
	l->update();
	lprintfln("process LCOM complete");
}

void LCOMIndex::pointerReleaseEvent(MAPoint2d point){

}

void LCOMIndex::pointerPressEvent(MAPoint2d point){
	Point p;
	p.set(point.x, point.y);

	if (mItems->contains(p)){
		for(int i=0; i< mItems->getChildren().size(); i++){
			if (mItems->getChildren()[i]->contains(p)){
				int index = mItems->getSelectedIndex();
				if (index == i) keyPressEvent(MAK_FIRE, 0);
				else mItems->setSelectedIndex(i);
				break;
			}
		}
	}
	else if (mSoftkey->contains(p)){
		if(mSoftkey->getChildren()[0]->contains(p)) keyPressEvent(MAK_SOFTLEFT, 0);
		else if (mSoftkey->getChildren()[2]->contains(p)) keyPressEvent(MAK_SOFTRIGHT, 0);
	}
}
